package com.yjzx.util.service.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;


/**
 * @author yjzx
 * @date 2023/11/02
 * @description 异常处理和响应处理
 */
@ControllerAdvice(basePackages = "com..controller")
public class ErrorHandler implements ResponseBodyAdvice<Object> {

    /**
     * 操作成功
     */
    private final static int SUCCESS_CODE = 0;
    /**
     * 操作失败
     */
    private final static int ERROR_CODE = -1;

    private final static int TOKEN_ERROR =  4001;
    private final static int PARAM_ERROR = 4002;


    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        Boolean isRest = AnnotationUtils.isAnnotationDeclaredLocally(
                RestController.class, returnType.getContainingClass());
        ResponseBody responseBody = AnnotationUtils.findAnnotation(
                returnType.getMethod(), ResponseBody.class);
        return isRest || responseBody != null;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (!(body instanceof Result)) {
            Result result = Result.getInstance();
            result.setData(body == null ? "" : body);
            return result;
        }
        return body;
    }

    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public Result handleAllException(Exception ex) {
        Result result = new Result();
        int ret = ERROR_CODE;
        String msg = ex.getMessage();

        result.setRet(ret);
        result.setMsg(msg);
        return result;
    }


    public static class Result {
        private String msg;
        private Integer ret;
        private Object data;


        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public Integer getRet() {
            return ret;
        }

        public void setRet(Integer ret) {
            this.ret = ret;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }


        public static Result getInstance() {
            Result result = new Result();

            result.setRet(SUCCESS_CODE);
            result.setMsg("请求成功");
            result.setData("");
            return result;
        }
    }
}
